package cn.cxy.demo.ui.activity

import androidx.lifecycle.Observer
import cn.cxy.demo.R
import cn.cxy.demo.base.BaseVmActivity
import cn.cxy.demo.network.response.Article
import cn.cxy.demo.ui.adapter.ArticleListAdapter
import cn.cxy.demo.viewmodel.ArticleListViewModel
import kotlinx.android.synthetic.main.activity_article_list.*

class ArticleListActivity : BaseVmActivity<ArticleListViewModel>() {
    private val adapter = ArticleListAdapter()
    override fun viewModelClass() = ArticleListViewModel::class.java
    override fun layoutResId(): Int = R.layout.activity_article_list

    override fun initView() {
        rv.adapter = adapter
        adapter.setOnItemClick(this::onItemClick)

        swipeRefreshLayout.setOnRefreshListener {
            initData()
        }
    }

    override fun initData() {
        mViewModel.loadArticleList(
                onSuccess = {
                    swipeRefreshLayout.isRefreshing = true
                },
                onComplete = {
                    swipeRefreshLayout.isRefreshing = false
                })
    }

    override fun observe() {
        mViewModel.dataList.observe(this, Observer { adapter.setData(it) })
    }

    private fun onItemClick(article: Article) {
    }
}
